home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / ping.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  5.1 KB  |  236 lines

  1. /* This includes the ping functions.. or the timeout functions */
  2. /* ----------------------------------------------------------- */
  3.  
  4. #include "headers.h" /* all the important stuff */
  5.  
  6. /* fork child to start pinging */
  7. void startPings()
  8. {
  9.    int res;
  10.  
  11.    if (pinging == 0)
  12.    {
  13.       debug("pinging is not implemented due to previous error\n");
  14.       return;
  15.    }
  16.  
  17.    /* we're now connecting.. check for pings */
  18.    debug("now forking child to start pinging..\n");
  19.  
  20.    /* (void)signal(SIGCHLD, quit); */ /* die if pinging fails */
  21.    (void)signal(SIGCHLD, SIG_IGN);
  22.  
  23.    running = 1, newData = 0;
  24.  
  25.    chpid = fork();
  26.    if (chpid == ERROR)
  27.    {
  28.       error("error forking child: %s\n\n", strerror(errno));
  29.       quit(ERROR);
  30.    }
  31.  
  32.    else if (chpid == 0) 
  33.    {
  34.       /* 
  35.          (kill ourselves to prevent pinging)
  36.          exit(0); 
  37.          child = 0;
  38.       */
  39.  
  40.       child = 1, chpid = getpid();
  41.  
  42.       running = 0;
  43.  
  44.       (void)signal(SIGHUP, SIG_IGN);
  45.       (void)signal(SIGCHLD, SIG_IGN);
  46.       (void)signal(SIGUSR1, sighandler);
  47.  
  48.       while (running != 1) (void)sleep(1);
  49.       (void)signal(SIGUSR1, gotNewData);
  50.  
  51.       checkPings();                  /* child sits and waits for pings  */
  52.    }
  53.  
  54.    else
  55.    {   
  56.       char readbuf[MAXREADSIZE];
  57.  
  58.       child = 1;
  59.       /* chpid = 0, child = 0; */
  60.  
  61.       debug("(in child) sending START PINGS to server\n");
  62.       send_data("START PINGS\n"); /* tell server to start pinging us */
  63.  
  64.       /* FIX - add timeout */
  65.       while(1)
  66.       {
  67.          memset(readbuf, 0, sizeof(readbuf));
  68.          recv_data(readbuf, sizeof(readbuf));
  69.  
  70.          if (strncmp(readbuf, "YES PINGING", 11) == 0)
  71.          {
  72. #          if !defined(SUN) && !defined(BSD)
  73. #           ifdef _POSIX_SAVED_IDS
  74.             res = setuid(0);
  75. #           else
  76.             res = seteuid(0);
  77. #           endif
  78.  
  79.             if (res == ERROR)
  80.             {
  81.                error("error setting [e]uid: %s\n\n", strerror(errno)); 
  82.                quit(ERROR);
  83.             }
  84. #          endif
  85.  
  86.             res = kill(chpid, SIGUSR1);
  87.             if (res == ERROR) 
  88.                error("unable to send SIGUSR1 to ping process\n\n");
  89.  
  90. #          if !defined(SUN) && !defined(BSD)
  91.             if (pwd != NULL)
  92.             {
  93. #              ifdef _POSIX_SAVED_IDS
  94.                res = setuid(pwd->pw_uid);
  95. #              else
  96.                res = seteuid(pwd->pw_uid);
  97. #              endif
  98.  
  99.                if (res == ERROR)
  100.                {
  101.                   error("error setting [e]uid: %s\n\n", strerror(errno)); 
  102.                   quit(ERROR);
  103.                }
  104.             }
  105. #          endif
  106.  
  107.             if (debugging == 1)
  108.             {
  109.                (void)putchar('\n');
  110.                (void)write(dblogfd, "\n", 1);
  111.             }
  112.  
  113.             debug("got server's permission to ping\n");
  114.             break;
  115.          }
  116.  
  117.          else if (strncmp(readbuf, "NO PINGING", 10) == 0)
  118.          {
  119.             debug("server is not allowing pinging.. aborting pinger\n");
  120.  
  121.             killPinger();
  122.             child = 0, chpid = 0;
  123.  
  124.             break;
  125.          }
  126.       }
  127.    }
  128. }
  129.  
  130.  
  131. /* --------------------------------- */
  132.  
  133.  
  134. /* check for pings and respond with pongs */
  135. void checkPings()
  136. {
  137.    int count = 0;  /* prevent any overflows */
  138.  
  139.    char *dataptr, *valptr;
  140.  
  141.    char pingval[8];
  142.    char readbuf[MAXREADSIZE];
  143.  
  144.  
  145.    while(1) 
  146.    {
  147.       memset(pingval, 0, sizeof(pingval));
  148.       valptr = pingval, dataptr = NULL;
  149.  
  150.       (void)signal(SIGALRM, errTimeout);
  151.       (void)alarm(MAXTIMEOUT);
  152.  
  153.       gotAlrm = 0;
  154.  
  155.       while(1)
  156.       {
  157.          memset(readbuf, 0, sizeof(readbuf));
  158.          recv_data(readbuf, sizeof(readbuf));
  159.  
  160.          /* debug("(in getPings) data is: %s\n", readbuf); */
  161.  
  162.          if (strncmp(readbuf, "PING", 4) == 0) break;
  163.          else if (gotAlrm == 1)
  164.          {
  165.             error("timed out waiting for a ping..aborting\n\n");
  166.             quit(ERROR);
  167.          }
  168.       }
  169.  
  170.       dataptr = strstr(readbuf, "PING");
  171.       dataptr += 5; /* skip "PING " */ 
  172.  
  173.       count = 0;   
  174.       while ((*dataptr) && (*dataptr != ' ') && 
  175.              (isprint((int)*dataptr) != 0) && (count < (int)sizeof(pingval)))
  176.       {
  177.          *valptr++ = *dataptr++;
  178.          count++;
  179.       }
  180.  
  181.       /* debug("ping value: %s\n", pingval); */
  182.       send_data("PONG %s", pingval);
  183.  
  184.       debug("(in child) going to top of loop for next ping\n\n");
  185.    }
  186. }
  187.  
  188.  
  189. /* -------------------------------- */
  190.  
  191.  
  192. /* kill the ping process */
  193. void killPinger()
  194. {
  195.    int res;
  196.    debug("now killing the ping process (pid %d)\n", chpid);
  197.  
  198. # if !defined(SUN) && !defined(BSD)
  199. #  ifdef _POSIX_SAVED_IDS
  200.    res = setuid(0);
  201. #  else
  202.    res = seteuid(0);
  203. #  endif
  204.  
  205.    if (res == ERROR)
  206.    {
  207.       error("error setting [e]uid: %s\n\n", strerror(errno)); 
  208.       quit(ERROR);
  209.    }
  210. # endif
  211.  
  212.    res = kill(chpid, SIGTERM);
  213.    if (res == ERROR)
  214.       error("error killing the ping process: %s\n\n", strerror(errno));
  215.  
  216.    if (pwd != NULL)
  217.    {
  218. #    if !defined(SUN) && !defined(BSD)
  219. #     ifdef _POSIX_SAVED_IDS
  220.       res = setuid(pwd->pw_uid);
  221. #     else
  222.       res = seteuid(pwd->pw_uid);
  223. #     endif
  224.  
  225.       if (res == ERROR)
  226.       {
  227.          error("error setting [e]uid: %s\n\n", strerror(errno)); 
  228.          quit(ERROR);
  229.       }
  230. #    endif
  231.    }
  232.  
  233.    child = chpid = 0;
  234. }
  235.  
  236.